//
//  ImageBroswerViewController.m
//  jizubao
//
//  Created by Davy on 2017/10/13.
//  Copyright © 2017年 Davy. All rights reserved.
//

#import "ImageBroswerViewController.h"

@interface ImageBroswerViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UILabel *numberLabel;

@property (nonatomic, strong) NSArray *imageArr;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *scrollViewArr; //放scrollView的数组
@property (nonatomic, strong) NSMutableArray *imageViewArr; //放图片视图的数组
@end

@implementation ImageBroswerViewController

-(instancetype)initWithImageArr:(NSArray *)imageArr{
    if (self = [super init]) {
        _imageArr = [imageArr copy];
    }
    return self;
}
-(NSMutableArray *)scrollViewArr{
    if (!_scrollViewArr) {
        _scrollViewArr = [NSMutableArray array];
    }
    return _scrollViewArr;
}

-(NSMutableArray *)imageViewArr{
    if (!_imageViewArr) {
        _imageViewArr = [NSMutableArray array];
    }
    return  _imageViewArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (!_imageArr || _imageArr.count == 0) {
        [MBProgressHUD showError:@"请传入有效数组" toView:nil];
        return;
    }
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT)];
    
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(DEVICE_SCREEN_WIDTH*_imageArr.count, DEVICE_SCREEN_WIDTH);
    _scrollView.backgroundColor = [UIColor blackColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_SCREEN_WIDTH-60, SafeAreaTopHeight-44, 44, 44)];
    [closeBtn setImage:[UIImage imageNamed:@"closeWhite"] forState:UIControlStateNormal];
    closeBtn.layer.cornerRadius = 22;
    closeBtn.layer.masksToBounds = YES;
    closeBtn.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0.5];
    [self.view addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    //遍历数组，展示图片
    for (int i = 0; i<_imageArr.count; i++) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(DEVICE_SCREEN_WIDTH*i, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT)];
        scrollView.maximumZoomScale = 2;
        scrollView.minimumZoomScale = 1;
        scrollView.delegate = self;
        [self.scrollViewArr addObject:scrollView];
        scrollView.contentSize = CGSizeMake(DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT);
        [_scrollView addSubview:scrollView];
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT)];
        [self.imageViewArr addObject:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [scrollView addSubview:imageView];
        id image = _imageArr[i];
        if ([image isKindOfClass:[UIImage class]]) {
            imageView.image = image;
        }
        if ([image isKindOfClass:[NSString class]]) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:image]];
        }
    }

    
    UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_SCREEN_WIDTH-68, DEVICE_SCREEN_HEIGHT-50, 48, 26)];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.view addSubview:saveBtn];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    saveBtn.layer.masksToBounds = YES;
    saveBtn.layer.cornerRadius = 4;
    saveBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    saveBtn.layer.borderWidth = 0.8;
    [saveBtn addTarget:self action:@selector(saveImage) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView setContentOffset:CGPointMake(DEVICE_SCREEN_WIDTH*_index, 0)];
    
    _numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, DEVICE_SCREEN_HEIGHT-48, DEVICE_SCREEN_WIDTH, 24)];
    [self.view addSubview:_numberLabel];
    _numberLabel.font = [UIFont systemFontOfSize:14];
    _numberLabel.textAlignment = NSTextAlignmentCenter;
    _numberLabel.textColor = [UIColor whiteColor];
    _numberLabel.text = [NSString stringWithFormat:@"%ld/%ld", self.index+1, _imageArr.count];
}



-(void)saveImage{
//    [MobClick event:@"e_4_8"];
    NSInteger index = _scrollView.contentOffset.x / DEVICE_SCREEN_WIDTH;
    NSLog(@"%ld", index);
    id image = _imageArr[index];
    if ([image isKindOfClass:[NSString class]]) {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL  URLWithString:image]];
        UIImage *image = [UIImage imageWithData:data];
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [MBProgressHUD showSuccess:@"保存失败" toView:self.view];
    } else {
        [MBProgressHUD showSuccess:@"保存成功" toView:self.view];
    }
}

-(void)setIndex:(NSInteger)index{
    _index = index;
    
    [_scrollView setContentOffset:CGPointMake(DEVICE_SCREEN_WIDTH*index, 0)];
}

-(void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        NSInteger currentIndex = scrollView.contentOffset.x/DEVICE_SCREEN_WIDTH + 1;
        _numberLabel.text = [NSString stringWithFormat:@"%ld/%ld", currentIndex, _imageArr.count];
       
    }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        NSInteger currentIndex = scrollView.contentOffset.x/DEVICE_SCREEN_WIDTH + 1;
        _numberLabel.text = [NSString stringWithFormat:@"%ld/%ld", currentIndex, _imageArr.count];
    }
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        return nil;
    }else{
        NSInteger index = [_scrollViewArr indexOfObject:scrollView];
        UIImageView *imageView = _imageViewArr[index];
        return imageView;
    }
    return nil;
}

@end
